package com.blockchain.console.service.content.achieve;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.security.auth.login.LoginException;

import com.blockchain.common.enums.ArticleType;
import com.blockchain.common.enums.IsPass;
import com.blockchain.console.config.ConsoleConst;
import com.blockchain.console.service.content.ArticleManage;
import com.blockchain.console.service.content.entity.Article;
import com.blockchain.console.service.content.entity.ArticleRecord;
import com.blockchain.console.service.content.query.ArticleQuery;
import com.blockchain.framework.data.sql.SQLConnectionProvider;
import com.blockchain.framework.http.upload.FileStore;
import com.blockchain.framework.http.upload.UploadFile;
import com.blockchain.framework.service.ServiceFactory;
import com.blockchain.framework.service.ServiceResource;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.framework.service.query.ArrayParser;
import com.blockchain.framework.service.query.ItemParser;
import com.blockchain.framework.service.query.Paging;
import com.blockchain.framework.service.query.PagingResult;
import com.blockchain.util.StringHelper;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.variables.FileType;
import com.blockchain.variables.P2PConst;

@SuppressWarnings("static-access")
public class ArticleManageImpl extends AbstractInformationService implements ArticleManage {

	public static class ArticleManageFactory implements ServiceFactory<ArticleManage> {
		@Override
		public ArticleManage newInstance(ServiceResource serviceResource) {
			return new ArticleManageImpl(serviceResource);
		}
	}

	protected static final ArrayParser<ArticleRecord> ARRAY_PARSER = new ArrayParser<ArticleRecord>() {

		@Override
		public ArticleRecord[] parse(ResultSet rs) throws SQLException {
			ArrayList<ArticleRecord> list = null;
			while (rs.next()) {
				ArticleRecord news = new ArticleRecord();
				news.id = rs.getInt(1);
				news.articleType = ArticleType.valueOf(rs.getString(2));
				news.viewTimes = rs.getInt(3);
				news.sortIndex = rs.getInt(4);
				news.publishStatus = IsPass.valueOf(rs.getString(5));
				news.title = rs.getString(6);
				news.source = rs.getString(7);
				news.summary = rs.getString(8);
				news.imageCode = rs.getString(9);
				news.publisherId = rs.getInt(10);
				news.createtime = rs.getTimestamp(11);
				news.publishTime = rs.getTimestamp(12);
				news.publisherName = rs.getString(13);
				news.isPass = EnumParser.parse(IsPass.class, rs.getString(14));
				news.isorder = rs.getInt(15);
				news.titleEn = rs.getString(16);
				news.titleTw = rs.getString(17);

				if (list == null) {
					list = new ArrayList<>();
				}
				list.add(news);
			}
			return list == null ? null : list.toArray(new ArticleRecord[list.size()]);
		}
	};
	protected static final ItemParser<ArticleRecord> ITEM_PARSER = new ItemParser<ArticleRecord>() {

		@Override
		public ArticleRecord parse(ResultSet rs) throws SQLException {
			ArticleRecord news = null;
			if (rs.next()) {
				news = new ArticleRecord();
				news.id = rs.getInt(1);
				news.articleType = ArticleType.valueOf(rs.getString(2));
				news.viewTimes = rs.getInt(3);
				news.sortIndex = rs.getInt(4);
				news.publishStatus = IsPass.valueOf(rs.getString(5));
				news.title = rs.getString(6);
				news.source = rs.getString(7);
				news.summary = rs.getString(8);
				news.imageCode = rs.getString(9);
				news.publisherId = rs.getInt(10);
				news.createtime = rs.getTimestamp(11);
				news.publishTime = rs.getTimestamp(12);
				news.publisherName = rs.getString(13);
				news.isPass = EnumParser.parse(IsPass.class, rs.getString(14));
				news.isorder = rs.getInt(15);
				news.titleEn = rs.getString(16);
				news.titleTw = rs.getString(17);
			}
			return news;
		}
	};
	protected static final String SELECT_ALL_SQL = "SELECT T5011.F01,T5011.F02,T5011.F03,T5011.F04,T5011.F05,T5011.F06,T5011.F07,T5011.F08,T5011.F09,T5011.F10,T5011.F11,T5011.F12,T7011.F04 AS F13,T5011.F14 ISPSS,T5011.F15 ISPSSORDER,T5011.F16 AS F16,T5011.F17 AS F17 FROM T5011 INNER JOIN "
			+ ConsoleConst.DB_CONSOLE + ".T7011 ON T5011.F10 = T7011.F01";

	public ArticleManageImpl(ServiceResource serviceResource) {
		super(serviceResource);
	}

	@Override
	public PagingResult<ArticleRecord> search(ArticleQuery query, Paging paging) throws Throwable {
		List<Object> parameters = new ArrayList<>();
		StringBuilder sql = new StringBuilder(SELECT_ALL_SQL);
		sql.append(" WHERE 1=1 AND (T5011.F02=? OR T5011.F02=?) ");
		parameters.add(ArticleType.ZCZX.toString());
		parameters.add(ArticleType.ZXZX.toString());
		if (query != null) {
			ArticleType articleType = query.getType();
			SQLConnectionProvider sqlConnectionProvider = getSQLConnectionProvider();
			if (articleType != null) {
				sql.append(" AND T5011.F02 = ?");
				parameters.add(articleType.name());
			}
			IsPass publishStatus = query.getPublishStatus();
			if (publishStatus != null) {
				sql.append(" AND T5011.F05 = ?");
				parameters.add(publishStatus.name());
			}
			String string = query.getTitle();
			if (!StringHelper.isEmpty(string)) {
				sql.append(" AND T5011.F06 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(string));
			}
			Timestamp timestamp = query.getCreateTimeStart();
			if (timestamp != null) {
				sql.append(" AND DATE(T5011.F11) >= ?");
				parameters.add(timestamp);
			}
			timestamp = query.getCreateTimeEnd();
			if (timestamp != null) {
				sql.append(" AND DATE(T5011.F11) <= ?");
				parameters.add(timestamp);
			}
			timestamp = query.getPublishTimeStart();
			if (timestamp != null) {
				sql.append(" AND DATE(T5011.F12) >= ?");
				parameters.add(timestamp);
			}
			timestamp = query.getPublishTimeEnd();
			if (timestamp != null) {
				sql.append(" AND DATE(T5011.F12) <= ?");
				parameters.add(timestamp);
			}
			string = query.getSource();
			if (!StringHelper.isEmpty(string)) {
				sql.append(" AND T5011.F07 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(string));
			}
			string = query.getSummary();
			if (!StringHelper.isEmpty(string)) {
				sql.append(" AND T5011.F08 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(string));
			}
			string = query.getPublisherName();
			if (!StringHelper.isEmpty(string)) {
				sql.append(" AND T7011.F04 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(string));
			}
		}
		sql.append(" ORDER BY F04 DESC,F11 DESC");
		return selectPaging(getConnection(), ARRAY_PARSER, paging, sql.toString(), parameters);
	}

	@Override
	public ArticleRecord get(int id) throws Throwable {
		if (id <= 0) {
			return null;
		}
		StringBuffer sql = new StringBuffer();
		sql.append(SELECT_ALL_SQL);
		sql.append(" WHERE T5011.F01  =?");
		return select(getConnection(), ITEM_PARSER, sql.toString(), id);
	}

	@Override
	public String getContent(int id) throws Throwable {
		if (id <= 0) {
			return null;
		}
		try (Connection connection = getConnection();
				PreparedStatement pstmt = connection.prepareStatement("SELECT F02 FROM T5011_1 WHERE F01 = ?")) {
			pstmt.setInt(1, id);
			try (ResultSet resultSet = pstmt.executeQuery()) {
				if (resultSet.next()) {
					return resultSet.getString(1);
				}
			}
		}
		return null;
	}
	@Override
	public String getContentEn(int id) throws Throwable {
		if (id <= 0) {
			return null;
		}
		try (Connection connection = getConnection();
				PreparedStatement pstmt = connection.prepareStatement("SELECT F03 FROM T5011_1 WHERE F01 = ?")) {
			pstmt.setInt(1, id);
			try (ResultSet resultSet = pstmt.executeQuery()) {
				if (resultSet.next()) {
					return resultSet.getString(1);
				}
			}
		}
		return null;
	}
	@Override
	public String getContentTw(int id) throws Throwable {
		if (id <= 0) {
			return null;
		}
		try (Connection connection = getConnection();
				PreparedStatement pstmt = connection.prepareStatement("SELECT F04 FROM T5011_1 WHERE F01 = ?")) {
			pstmt.setInt(1, id);
			try (ResultSet resultSet = pstmt.executeQuery()) {
				if (resultSet.next()) {
					return resultSet.getString(1);
				}
			}
		}
		return null;
	}

	@Override
	public void setPublishStatus(IsPass publishStatus, int... ids) throws Throwable {
		if (ids == null || ids.length == 0 || publishStatus == null) {
			return;
		}
		Timestamp now = new Timestamp(System.currentTimeMillis());
		try (Connection connection = getConnection()) {
			try (PreparedStatement pstmt = connection
					.prepareStatement("UPDATE T5011 SET F05  = ?, F12  = ? WHERE F01 = ? AND F05 = ?")) {
				for (int id : ids) {
					if (id <= 0) {
						continue;
					}
					pstmt.setString(1, publishStatus.name());
					pstmt.setTimestamp(2, now);
					pstmt.setInt(3, id);
					pstmt.setString(4, publishStatus == IsPass.S ? IsPass.F.name() : IsPass.S.name());
					pstmt.addBatch();
				}
				pstmt.executeBatch();
			}
		}
	}

	@Override
	public void setTop(boolean top, int... ids) throws Throwable {
		if (ids == null || ids.length == 0) {
			return;
		}
		int sortIndex = top ? Integer.MAX_VALUE : 0;
		Timestamp now = new Timestamp(System.currentTimeMillis());
		try (Connection connection = getConnection()) {
			try (PreparedStatement pstmt = connection
					.prepareStatement("UPDATE T5011 SET F04  = ?, F12  = ? WHERE F01 = ? ")) {
				for (int id : ids) {
					if (id <= 0) {
						continue;
					}
					pstmt.setInt(1, sortIndex);
					pstmt.setTimestamp(2, now);
					pstmt.setInt(3, id);
					pstmt.addBatch();
				}
				pstmt.executeBatch();
			}
		}
	}

	@Override
	public void delete(int... ids) throws Throwable {
		if (ids == null || ids.length == 0) {
			return;
		}
		serviceResource.openTransactions();
		try (Connection connection = getConnection()) {
			// 删除附件列表
			try (PreparedStatement pstmt = connection.prepareStatement("DELETE FROM T5011_2 WHERE F01 = ?")) {
				for (int id : ids) {
					if (id <= 0) {
						continue;
					}
					pstmt.setInt(1, id);
					pstmt.addBatch();
				}
				pstmt.executeBatch();
			}
			// 删除文章内容
			try (PreparedStatement pstmt = connection.prepareStatement("DELETE FROM T5011_1 WHERE F01 = ?")) {
				for (int id : ids) {
					if (id <= 0) {
						continue;
					}
					pstmt.setInt(1, id);
					pstmt.addBatch();
				}
				pstmt.executeBatch();
			}
			// 删除文章记录
			try (PreparedStatement pstmt = connection.prepareStatement("DELETE FROM T5011 WHERE F01 = ?")) {
				for (int id : ids) {
					if (id <= 0) {
						continue;
					}
					pstmt.setInt(1, id);
					pstmt.addBatch();
				}
				pstmt.executeBatch();
			}
		}
	}

	@Override
	public void update(int id, Article article, ArticleType type) throws Throwable {
		if (id <= 0 || article == null) {
			return;
		}
		if (type == null) {
			throw new ParameterException("没有指定文章类型");
		}
		String title;
		String titleEn = "";
		String titleTw = "";
		String source;
		String content = null;
		String contentEn = "";
		String contentTw = "";
		IsPass publishStatus = article.getPublishStatus();
		IsPass isPass = article.getIstj();
		{
			// 校验
			title = article.getTitle();
			if (StringHelper.isEmpty(title)) {
				throw new ParameterException("标题不能为空.");
			}
			if (title.length() > 60) {
				throw new ParameterException("标题不能超过60个字符.");
			}
			if(isMultiLang()) {
				titleEn = article.getTitleEn();
				if (StringHelper.isEmpty(titleEn)) {
					throw new ParameterException("标题(英文)不能为空.");
				}
				titleTw = article.getTitleTw();
				if (StringHelper.isEmpty(titleTw)) {
					throw new ParameterException("标题(繁体)不能为空.");
				}
				if (titleTw.length() > 60) {
					throw new ParameterException("标题(繁体)不能超过60个字符.");
				}
				contentEn = article.getContentEn();
				if (StringHelper.isEmpty(contentEn)) {
					throw new ParameterException("文章内容(英文)不能为空");
				}
				contentTw = article.getContentTw();
				if (StringHelper.isEmpty(contentTw)) {
					throw new ParameterException("文章内容(繁体)不能为空");
				}
			}
			source = article.getSource();
			if (!StringHelper.isEmpty(source) && source.length() > 50) {
				throw new ParameterException("文章来源不能超过50个字符.");
			}
			content = article.getContent();
			if (StringHelper.isEmpty(content)) {
				throw new ParameterException("文章内容不能为空");
			}
			if (isPass == null) {
				isPass = IsPass.F;
			}
		}

		serviceResource.openTransactions();
		try (Connection connection = getConnection()) {
			try (PreparedStatement pstmt = connection
					.prepareStatement("SELECT F01 FROM T5011 WHERE F01 = ? FOR UPDATE")) {
				pstmt.setInt(1, id);
				try (ResultSet resultSet = pstmt.executeQuery()) {
					if (!resultSet.next()) {
						throw new LoginException("文章不存在.");
					}
				}
			}
			// 修改记录信息
			execute(connection,
					"UPDATE T5011 SET F02=?, F04 = ?, F06 = ?, F16 = ?, F17 = ?, F07 = ?, F08  =?,F11= ? , F12 = ?,F14=?,F15=? WHERE F01 = ?",
					type, article.getSortIndex(), title, titleEn, titleTw, source, article.getSummary(),
					new Timestamp(System.currentTimeMillis()), article.publishTime(), isPass, article.getIsorder(), id);
			// 修改内容
			execute(connection, "UPDATE T5011_1 SET F02 = ? ,F03 = ?, F04 = ? WHERE F01 = ?", content, contentEn, contentTw, id);

			if (publishStatus != null) {
				setPublishStatus(publishStatus, id);
			}
		}
	}

	@Override
	public void update(int id, Article article, String type) throws Throwable {
		if (id <= 0 || article == null) {
			return;
		}
		if (StringHelper.isEmpty(type)) {
			throw new ParameterException("没有指定文章类型");
		}
		String title;
		String source;
		String content = null;
		String contentEn = "";
		String contentTw = "";
		IsPass publishStatus = article.getPublishStatus();
		IsPass isPass = article.getIstj();
		{
			// 校验
			title = article.getTitle();
			if (StringHelper.isEmpty(title)) {
				throw new ParameterException("标题不能为空.");
			}
			if (title.length() > 50) {
				throw new ParameterException("标题不能超过50个字符.");
			}
			source = article.getSource();
			if (!StringHelper.isEmpty(source) && source.length() > 50) {
				throw new ParameterException("文章来源不能超过50个字符.");
			}
			content = article.getContent();
			if (StringHelper.isEmpty(content)) {
				throw new ParameterException("文章内容不能为空");
			}
			if(isMultiLang()) {
				contentEn = article.getContentEn();
				if (StringHelper.isEmpty(contentEn)) {
					throw new ParameterException("文章内容(英文)不能为空");
				}
				contentTw = article.getContentTw();
				if (StringHelper.isEmpty(contentTw)) {
					throw new ParameterException("文章内容(繁体)不能为空");
				}
			}
			if (isPass == null) {
				isPass = IsPass.F;
			}
		}

		serviceResource.openTransactions();
		try (Connection connection = getConnection()) {
			try (PreparedStatement pstmt = connection
					.prepareStatement("SELECT F01 FROM T5011 WHERE F01 = ? FOR UPDATE")) {
				pstmt.setInt(1, id);
				try (ResultSet resultSet = pstmt.executeQuery()) {
					if (!resultSet.next()) {
						throw new LoginException("文章不存在.");
					}
				}
			}
			// 修改记录信息
			execute(connection,
					"UPDATE T5011 SET F02=?, F04 = ?, F06 = ?, F07 = ?, F08  =?,F11= ? , F12 = ?,F14=?,F15=? WHERE F01 = ?",
					type, article.getSortIndex(), title, source, article.getSummary(),
					new Timestamp(System.currentTimeMillis()), article.publishTime(), isPass, article.getIsorder(), id);
			// 修改内容
			execute(connection, "UPDATE T5011_1 SET F02 = ? ,F03 = ?, F04 = ? WHERE F01 = ?", content, contentEn, contentTw, id);

			if (publishStatus != null) {
				setPublishStatus(publishStatus, id);
			}
		}
	}

	@Override
	public int add(ArticleType articleType, Article article) throws Throwable {
		if (articleType == null) {
			throw new ParameterException("没有指定文章类型");
		}
		String title;
		String titleEn = "";
		String titleTw = "";
		String source;
		String content = null;
		String contentEn = "";
		String contentTw = "";
		IsPass publishStatus = null;
		IsPass isPass = article.getIstj();
		{
			// 校验
			title = article.getTitle();
			if (StringHelper.isEmpty(title)) {
				throw new ParameterException("标题不能为空.");
			}
			if (title.length() > 60) {
				throw new ParameterException("标题不能超过60个字符.");
			}
			if(isMultiLang()) {
				titleEn = article.getTitleEn();
				if (StringHelper.isEmpty(titleEn)) {
					throw new ParameterException("标题(英文)不能为空.");
				}
//				if (titleEn.length() > 60) {
//					throw new ParameterException("标题(英文)不能超过60个字符.");
//				}
				titleTw = article.getTitleTw();
				if (StringHelper.isEmpty(titleTw)) {
					throw new ParameterException("标题(繁体)不能为空.");
				}
				if (titleTw.length() > 60) {
					throw new ParameterException("标题(繁体)不能超过60个字符.");
				}
				contentEn = article.getContentEn();
				if (StringHelper.isEmpty(contentEn)) {
					throw new ParameterException("文章内容(英文)不能为空");
				}
				contentTw = article.getContentTw();
				if (StringHelper.isEmpty(contentTw)) {
					throw new ParameterException("文章内容(繁体)不能为空");
				}
			}
			source = article.getSource();
			if (!StringHelper.isEmpty(source) && source.length() > 50) {
				throw new ParameterException("文章来源不能超过50个字符.");
			}
			publishStatus = article.getPublishStatus();
			if (publishStatus == null) {
				publishStatus = isPass.F;
			}
			content = article.getContent();
			if (StringHelper.isEmpty(content)) {
				throw new ParameterException("文章内容不能为空");
			}
			if (isPass == null) {
				isPass = IsPass.F;
			}
		}

		serviceResource.openTransactions();
		try (Connection connection = getConnection()) {
			Timestamp now = new Timestamp(System.currentTimeMillis());
			int id = insert(connection,
					"INSERT INTO T5011 SET F02 = ?,F03 = ?,F04 = ?,F05 = ?,F06 = ?,F16 = ?,F17 = ?,F07 = ?,F08 = ?,F09 = ?,F10 = ?,F11 = ?,F12 = ?,F14=?,F15=?",
					articleType.name(), 0, article.getSortIndex(), publishStatus.name(), title, titleEn, titleTw, source,
					article.getSummary(), "", serviceResource.getSession().getAccountId(), now, article.publishTime(),
					isPass, article.getIsorder());
			execute(connection, "INSERT INTO T5011_1 SET F01 = ?, F02 = ?, F03 = ?, F04 = ?", 
					id, content, contentEn, contentTw);
			return id;
		}
	}

	@Override
	public int add(String articleType, Article article) throws Throwable {
		if (StringHelper.isEmpty(articleType)) {
			throw new ParameterException("没有指定文章类型");
		}
		String title;
		String source;
		String content = null;
		String contentEn = "";
		String contentTw = "";
		IsPass publishStatus = null;
		IsPass isPass = article.getIstj();
		{
			// 校验
			title = article.getTitle();
			if (StringHelper.isEmpty(title)) {
				throw new ParameterException("标题不能为空.");
			}
			if (title.length() > 50) {
				throw new ParameterException("标题不能超过50个字符.");
			}
			source = article.getSource();
			if (!StringHelper.isEmpty(source) && source.length() > 50) {
				throw new ParameterException("文章来源不能超过50个字符.");
			}
			publishStatus = article.getPublishStatus();
			if (publishStatus == null) {
				publishStatus = isPass.F;
			}
			content = article.getContent();
			if (StringHelper.isEmpty(content)) {
				throw new ParameterException("文章内容不能为空");
			}
			if(isMultiLang()) {
				contentEn = article.getContentEn();
				if (StringHelper.isEmpty(contentEn)) {
					throw new ParameterException("文章内容(英文)不能为空");
				}
				contentTw = article.getContentTw();
				if (StringHelper.isEmpty(contentTw)) {
					throw new ParameterException("文章内容(繁体)不能为空");
				}
			}
			if (isPass == null) {
				isPass = IsPass.F;
			}
		}

		serviceResource.openTransactions();
		try (Connection connection = getConnection()) {
			Timestamp now = new Timestamp(System.currentTimeMillis());
			int id = insert(connection,
					"INSERT INTO T5011 SET F02 = ?,F03 = ?,F04 = ?,F05 = ?,F06 = ?,F07 = ?,F08 = ?,F09 = ?,F10 = ?,F11 = ?,F12 = ?,F14=?,F15=?",
					articleType, 0, article.getSortIndex(), publishStatus.name(), title, source, article.getSummary(),
					"", serviceResource.getSession().getAccountId(), now, article.publishTime(), isPass,
					article.getIsorder());
			execute(connection, "INSERT INTO T5011_1 SET F01 = ?, F02 = ?, F03 = ?, F04 = ?", id, content, contentEn, contentTw);
			return id;
		}
	}

	@Override
	public String[] getAttachments(int id) throws Throwable {
		if (id <= 0) {
			return null;
		}
		try (Connection connection = getConnection()) {
			try (PreparedStatement pstmt = connection.prepareStatement("SELECT F02 FROM T5011_2 WHERE F01 = ? ")) {
				pstmt.setInt(1, id);
				try (ResultSet resultSet = pstmt.executeQuery()) {
					ArrayList<String> list = null;
					while (resultSet.next()) {
						if (list == null) {
							list = new ArrayList<>();
						}
						list.add(resultSet.getString(1));
					}
					return list == null ? null : list.toArray(new String[list.size()]);
				}
			}
		}
	}

	@Override
	public void addAttachments(int id, UploadFile... attachments) throws Throwable {
		if (id <= 0 || attachments == null || attachments.length == 0) {
			return;
		}
		serviceResource.openTransactions();
		try (Connection connection = getConnection()) {
			try (PreparedStatement pstmt = connection.prepareStatement("SELECT F01 FROM T5011 WHERE F01 = ?")) {
				pstmt.setInt(1, id);
				try (ResultSet resultSet = pstmt.executeQuery()) {
					if (!resultSet.next()) {
						throw new LoginException("文章不存在.");
					}
				}
			}
			FileStore fileStore = serviceResource.getResource(FileStore.class);
			String[] attachmentCodes = fileStore.upload(FileType.ARTICLE_ATTACHMENT.ordinal(), attachments);
			// 保存附件
			if (attachmentCodes != null) {
				try (PreparedStatement pstmt = connection
						.prepareStatement("INSERT INTO T5011_2 SET F01 = ? AND F02 = ?")) {
					for (String attachmentCode : attachmentCodes) {
						pstmt.setInt(1, id);
						pstmt.setString(2, attachmentCode);
						pstmt.addBatch();
					}
					pstmt.executeBatch();
				}
			}
		}
	}

	@Override
	public void deleteAttachments(int id, String... attachmentCodes) throws Throwable {
		if (id <= 0 || attachmentCodes == null || attachmentCodes.length == 0) {
			return;
		}
		serviceResource.openTransactions();
		try (Connection connection = getConnection()) {
			try (PreparedStatement pstmt = connection.prepareStatement("SELECT F01 FROM T5011 WHERE F01 = ? ")) {
				pstmt.setInt(1, id);
				try (ResultSet resultSet = pstmt.executeQuery()) {
					if (!resultSet.next()) {
						throw new LoginException("文章不存在.");
					}
				}
			}
			try (PreparedStatement pstmt = connection
					.prepareStatement("DELETE FROM T5011_2 WHERE F01 = ? AND F02 = ?")) {
				for (String attachmentCode : attachmentCodes) {
					if (StringHelper.isEmpty(attachmentCode)) {
						continue;
					}
					pstmt.setInt(1, id);
					pstmt.setString(2, attachmentCode);
					pstmt.addBatch();
				}
				pstmt.executeBatch();
			}
		}
	}

	@Override
	public ArticleRecord get(ArticleType articleType) throws Throwable {
		if (articleType == null) {
			return null;
		}
		StringBuffer sql = new StringBuffer();
		sql.append(SELECT_ALL_SQL);
		sql.append(" WHERE T5011.F02 = ? LIMIT 1");
		return select(getConnection(), ITEM_PARSER, sql.toString(), articleType);
	}

	public int getId(String articleType) throws Throwable {
		int id = 0;
		if (StringHelper.isEmpty(articleType)) {
			return id;
		}
		return id = selectInt(P2PConst.DB_INFORMATION, "SELECT F01 FROM T5011 WHERE F02=?", articleType);
	}

	@Override
	public String getContent(String articleType) throws Throwable {
		if (articleType == null) {
			return null;
		}
		try (Connection connection = getConnection()) {
			int id;
			try (PreparedStatement pstmt = connection
					.prepareStatement("SELECT F01 FROM T5011 WHERE T5011.F02 = ? LIMIT 1")) {
				pstmt.setString(1, articleType);
				try (ResultSet resultSet = pstmt.executeQuery()) {
					if (resultSet.next()) {
						id = resultSet.getInt(1);
					} else {
						return null;
					}
				}
			}
			try (PreparedStatement pstmt = connection.prepareStatement(
					"SELECT T5011_1.F02 FROM T5011_1 INNER JOIN T5011 ON T5011.F01 = T5011_1.F01 WHERE T5011_1.F01 = ? ")) {
				pstmt.setInt(1, id);
				try (ResultSet resultSet = pstmt.executeQuery()) {
					if (resultSet.next()) {
						return resultSet.getString(1);
					}
				}
			}
		}
		return null;
	}
	
	@Override
	public String getContentEn(String articleType) throws Throwable {
		if (articleType == null) {
			return null;
		}
		try (Connection connection = getConnection()) {
			int id;
			try (PreparedStatement pstmt = connection
					.prepareStatement("SELECT F01 FROM T5011 WHERE T5011.F02 = ? LIMIT 1")) {
				pstmt.setString(1, articleType);
				try (ResultSet resultSet = pstmt.executeQuery()) {
					if (resultSet.next()) {
						id = resultSet.getInt(1);
					} else {
						return null;
					}
				}
			}
			try (PreparedStatement pstmt = connection.prepareStatement(
					"SELECT T5011_1.F03 FROM T5011_1 INNER JOIN T5011 ON T5011.F01 = T5011_1.F01 WHERE T5011_1.F01 = ? ")) {
				pstmt.setInt(1, id);
				try (ResultSet resultSet = pstmt.executeQuery()) {
					if (resultSet.next()) {
						return resultSet.getString(1);
					}
				}
			}
		}
		return null;
	}
	
	@Override
	public String getContentTw(String articleType) throws Throwable {
		if (articleType == null) {
			return null;
		}
		try (Connection connection = getConnection()) {
			int id;
			try (PreparedStatement pstmt = connection
					.prepareStatement("SELECT F01 FROM T5011 WHERE T5011.F02 = ? LIMIT 1")) {
				pstmt.setString(1, articleType);
				try (ResultSet resultSet = pstmt.executeQuery()) {
					if (resultSet.next()) {
						id = resultSet.getInt(1);
					} else {
						return null;
					}
				}
			}
			try (PreparedStatement pstmt = connection.prepareStatement(
					"SELECT T5011_1.F04 FROM T5011_1 INNER JOIN T5011 ON T5011.F01 = T5011_1.F01 WHERE T5011_1.F01 = ? ")) {
				pstmt.setInt(1, id);
				try (ResultSet resultSet = pstmt.executeQuery()) {
					if (resultSet.next()) {
						return resultSet.getString(1);
					}
				}
			}
		}
		return null;
	}

	@Override
	public ArticleRecord get(int id, ArticleType articleType) throws Throwable {
		if (id <= 0 || articleType == null) {
			return null;
		}
		StringBuffer sql = new StringBuffer();
		sql.append(SELECT_ALL_SQL);
		sql.append(" WHERE T5011.F01  =? AND T5011.F02 = ?");
		return select(getConnection(), ITEM_PARSER, sql.toString(), id, articleType);
	}

	@Override
	public PagingResult<ArticleRecord> searchTj(ArticleQuery query, Paging paging) throws Throwable {
		StringBuilder sql = new StringBuilder(SELECT_ALL_SQL);
		sql.append(" WHERE 1=1 ");
		List<Object> parameters = new ArrayList<>();
		if (query != null) {
			SQLConnectionProvider sqlConnectionProvider = getSQLConnectionProvider();
			sql.append(" AND T5011.F14 = ?");
			parameters.add(IsPass.S);
			IsPass publishStatus = query.getPublishStatus();
			if (publishStatus != null) {
				sql.append(" AND T5011.F05 = ?");
				parameters.add(publishStatus.name());
			}
			String string = query.getTitle();
			if (!StringHelper.isEmpty(string)) {
				sql.append(" AND T5011.F06 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(string));
			}
			Timestamp timestamp = query.getCreateTimeStart();
			if (timestamp != null) {
				sql.append(" AND DATE(T5011.F11) >= ?");
				parameters.add(timestamp);
			}
			timestamp = query.getCreateTimeEnd();
			if (timestamp != null) {
				sql.append(" AND DATE(T5011.F11) <= ?");
				parameters.add(timestamp);
			}
			timestamp = query.getPublishTimeStart();
			if (timestamp != null) {
				sql.append(" AND DATE(T5011.F12) >= ?");
				parameters.add(timestamp);
			}
			timestamp = query.getPublishTimeEnd();
			if (timestamp != null) {
				sql.append(" AND DATE(T5011.F12) <= ?");
				parameters.add(timestamp);
			}
			string = query.getSource();
			if (!StringHelper.isEmpty(string)) {
				sql.append(" AND T5011.F07 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(string));
			}
			string = query.getSummary();
			if (!StringHelper.isEmpty(string)) {
				sql.append(" AND T5011.F08 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(string));
			}
			string = query.getPublisherName();
			if (!StringHelper.isEmpty(string)) {
				sql.append(" AND T7011.F04 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(string));
			}
		}
		sql.append(" ORDER BY F15 DESC,F11 DESC");
		return selectPaging(getConnection(), ARRAY_PARSER, paging, sql.toString(), parameters);
	}

	@Override
	public void gotoType(ArticleType parse, int... ids) throws Throwable {
		if (ids == null || ids.length == 0 || parse == null) {
			return;
		}
		serviceResource.openTransactions();
		try (Connection connection = getConnection()) {
			// 删除附件列表
			try (PreparedStatement pstmt = connection.prepareStatement("UPDATE T5011 SET F02=?  WHERE F01 = ?")) {
				for (int id : ids) {
					if (id <= 0) {
						continue;
					}
					pstmt.setString(1, parse.name());
					pstmt.setInt(2, id);
					pstmt.addBatch();
				}
				pstmt.executeBatch();
			}
		}
	}
	
	public boolean isMultiLang() {
		return this.serviceResource.getSystemDefine().isMulitLanguage();
	}
}
